package iamutkarshtiwari.github.io.ananas.editimage.fragment;

import iamutkarshtiwari.github.io.ananas.ResourceTable;
import iamutkarshtiwari.github.io.ananas.editimage.EditImageAbility;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;

import java.io.IOException;
import java.util.List;


public class MainMenuFragment extends ComponentContainer implements Component.ClickedListener {
    private Context mContext;
    DirectionalLayout first, second, three, four, five, six, seven, eight, nine;
//    DirectionalLayout mainView;
    PageSlider pageSlider;
//    private List<ComponentContainer> list;
    EditImageAbility editImageAbility;
//    private int mScrollX;
//    private ScrollView scrollView;

    public void setComponentContainer(List<ComponentContainer> list) {
//        this.list = list;
    }

    public MainMenuFragment(Context context, PageSlider pageSlider) {
        super(context);
        mContext = context;
        editImageAbility = (EditImageAbility) context;
        this.pageSlider = pageSlider;
        initView();
    }

    public MainMenuFragment(Context context, AttrSet attrSet) {
        super(context, attrSet);
        mContext = context;
        initView();
    }

    public MainMenuFragment(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }


    private void initView() {
        Component component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_fragment_main_layout, null, false);
        addComponent(component);
//        scrollView = (ScrollView) component.findComponentById(ResourceTable.Id_scroll_view);
        first = (DirectionalLayout) component.findComponentById(ResourceTable.Id_btn_paint);
        second = (DirectionalLayout) component.findComponentById(ResourceTable.Id_btn_filter);
        three = (DirectionalLayout) component.findComponentById(ResourceTable.Id_btn_crop);
        four = (DirectionalLayout) component.findComponentById(ResourceTable.Id_btn_rotate);
        five = (DirectionalLayout) component.findComponentById(ResourceTable.Id_btn_text);
        six = (DirectionalLayout) component.findComponentById(ResourceTable.Id_btn_stickers);
        seven = (DirectionalLayout) component.findComponentById(ResourceTable.Id_btn_brightness);
        eight = (DirectionalLayout) component.findComponentById(ResourceTable.Id_btn_contrast);
        nine = (DirectionalLayout) component.findComponentById(ResourceTable.Id_btn_beauty);
        Image image = (Image) component.findComponentById(ResourceTable.Id_paint_icon);
        try {
            image.setPixelMap(ImageSource.create(getResourceManager().getResource(ResourceTable.Media_paint), new ImageSource.SourceOptions()).createPixelmap(null));
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }

        first.setClickedListener(this);
        second.setClickedListener(this);
        three.setClickedListener(this);
        four.setClickedListener(this);
        five.setClickedListener(this);
        six.setClickedListener(this);
        seven.setClickedListener(this);
        eight.setClickedListener(this);
        nine.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        //拦截穿透
        if (pageSlider.getVisibility() == VISIBLE) {
            return;
        }
        pageSlider.setVisibility(VISIBLE);
        int id = component.getId();
        if (id == ResourceTable.Id_btn_paint) {
            editImageAbility.saveOrApply(true);
            editImageAbility.setPaintFragmentVisible();
            pageSlider.setCurrentPage(0, false);
        } else if (id == ResourceTable.Id_btn_filter) {
            editImageAbility.saveOrApply(true);
            editImageAbility.setFilterListFragmentVisible();
            pageSlider.setCurrentPage(1, false);
        } else if (id == ResourceTable.Id_btn_crop) {
            editImageAbility.saveOrApply(true);
            editImageAbility.setCropFragmentVisible();
            pageSlider.setCurrentPage(2, false);
        } else if (id == ResourceTable.Id_btn_rotate) {
            editImageAbility.saveOrApply(true);
            editImageAbility.setRotateFragmentVisible();
            pageSlider.setCurrentPage(3, false);
        } else if (id == ResourceTable.Id_btn_text) {
            editImageAbility.saveOrApply(true);
            editImageAbility.setAddTextFragmentVisible();
            pageSlider.setCurrentPage(4, false);
        } else if (id == ResourceTable.Id_btn_stickers) {
            editImageAbility.saveOrApply(true);
            editImageAbility.setStickerFragmentVisible();
            pageSlider.setCurrentPage(5, false);
        } else if (id == ResourceTable.Id_btn_brightness) {
            editImageAbility.saveOrApply(true);
            editImageAbility.setBrightnessFragmentVisible();
            pageSlider.setCurrentPage(6, false);
        } else if (id == ResourceTable.Id_btn_contrast) {
            editImageAbility.saveOrApply(true);
            editImageAbility.setSaturationFragmentVisible();
            pageSlider.setCurrentPage(7, false);
        } else if (id == ResourceTable.Id_btn_beauty) {
            editImageAbility.saveOrApply(true);
            editImageAbility.setBeautyFragmentVisible();
            pageSlider.setCurrentPage(8, false);
        }
    }

    public void clear() {
        editImageAbility = null;
        mContext = null;
        pageSlider = null;
    }

}
